package com.lhf.fvscommon.result;

import lombok.Data;

import java.io.Serializable;

/**
 * <p></p>
 *
 * @author lhf
 * @since 2020/10/22 10:26
 */

@Data
public class Result<T> {

    private int code;

    private T t;

    private String mes;

    public Result(int code, T t, String mes) {
        this.code = code;
        this.t = t;
        this.mes = mes;
    }


    public static <T> Result<T> success() {
        Status success = Status.SUCCESS;
        return new Result<>(success.code, null, success.mes);
    }

    /**
     * 请求成功默认返回
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T t) {
        Status success = Status.SUCCESS;
        return new Result<>(success.code, t, success.mes);
    }

    /**
     * 请求成功自定义状态返回
     *
     * @param t
     * @param status
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T t, Status status) {
        return new Result<>(status.code, t, status.mes);
    }

    /**
     * 请求失败默认返回
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> failure() {
        Status failure = Status.FAILURE;
        return new Result<>(failure.code, null, failure.mes);
    }

    /**
     * 自定义失败返回状态，返回信息
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> failure(Status status, String mes) {
        return new Result<>(status.code, null, mes);
    }

    /**
     * 请求失败自定义状态
     *
     * @param status
     * @param <T>
     * @return
     */
    public static <T> Result<T> failure(Status status) {
        return new Result<>(status.code, null, status.mes);
    }


    /**
     * 构建
     *
     * @param <T>
     * @return
     */
    public static <T> Builder<T> builder() {
        return new Builder<>();
    }

    public static class Builder<T> {
        private int code;
        private T t;
        private String mes;

        public Builder<T> code(int code) {
            this.code = code;
            return this;
        }

        public Builder<T> t(T t) {
            this.t = t;
            return this;
        }

        public Builder<T> mes(String mes) {
            this.mes = mes;
            return this;
        }

        public Result<T> build() {
            return new Result<>(this.code, this.t, this.mes);
        }
    }
}
